package com.example.italkapp2.controller.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.italkapp2.R;
import com.example.italkapp2.model.bean.UserInfo;
import com.example.italkapp2.utils.Constant;

import java.util.ArrayList;
import java.util.List;

public class GroupDetailAdapter extends BaseAdapter {
    private Context mContext;
    private boolean isCanModify;//是否允许添加和删除群成员
    private List<UserInfo> mUsrs=new ArrayList<>();
    private boolean mIsDeleteModel;//删除模式，true表示可以删除，false表示不能删除
    private OnGroupDetailListener mOnGroupDetailListener;
    private UserInfo userInfo;

    public GroupDetailAdapter(Context mContext, boolean isCanModify,OnGroupDetailListener mOnGroupDetailListener) {
        this.mContext = mContext;
        this.isCanModify = isCanModify;
        this.mOnGroupDetailListener=mOnGroupDetailListener;
    }

    //刷新数据
    public void refresh(List<UserInfo> userInfos){
        if(userInfos!=null && userInfos.size()>=0){
            mUsrs.clear();

            //添加加号减号
            initUsers();
            //所有用户信息都从0位置开始，把先来的数据往后推，这样加号和减号就能一直出现在后头
            mUsrs.addAll(0,userInfos);
        }
        notifyDataSetChanged();
    }

    private void initUsers() {
        UserInfo add= new UserInfo("add");
        UserInfo delete=new UserInfo("delete");
        mUsrs.add(delete);
        mUsrs.add(0,add);//加号把减号往后推了


    }

    @Override
    public int getCount() {
        return mUsrs==null?0:mUsrs.size();
    }

    @Override
    public Object getItem(int position) {
        return mUsrs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获取或者创建viewHolder
        ViewHolder holder=null;

        if(convertView==null){
            holder=new ViewHolder();
            convertView=View.inflate(mContext, R.layout.item_groupdetail,null);
            holder.photo=convertView.findViewById(R.id.iv_group_detail_photo);
            holder.delete=convertView.findViewById(R.id.iv_group_detail_delete);
            holder.name=convertView.findViewById(R.id.tv_group_detail_name);

            convertView.setTag(holder);
        }else {
            holder=(ViewHolder) convertView.getTag();
        }

        //获取当前item数据
        userInfo=mUsrs.get(position);

        //显示数据
        if(isCanModify){//表示群主或者开放了群权限

            //布局处理
            if(position==getCount()-1){//减号处理
                //是否进入删除模式
                if(mIsDeleteModel){
                    convertView.setVisibility(View.INVISIBLE);
                }else {
                    convertView.setVisibility(View.VISIBLE);
                    holder.photo.setImageResource(R.drawable.em_smiley_minus_btn_pressed);
                    holder.delete.setVisibility(View.GONE);
                    holder.name.setVisibility(View.INVISIBLE);
                }

            }else if(position==getCount()-2){//加号处理
                if(mIsDeleteModel){
                    convertView.setVisibility(View.INVISIBLE);
                }else {
                    convertView.setVisibility(View.VISIBLE);
                    holder.photo.setImageResource(R.drawable.em_smiley_add_btn_pressed);
                    holder.delete.setVisibility(View.GONE);
                    holder.name.setVisibility(View.INVISIBLE);
                }

            }else {//群成员处理
                convertView.setVisibility(View.VISIBLE);
                holder.name.setVisibility(View.VISIBLE);
                holder.name.setText(userInfo.getName());
                holder.photo.setImageResource(R.drawable.em_default_avatar);
                if(mIsDeleteModel){
                    holder.delete.setVisibility(View.VISIBLE);
                }else {
                    holder.delete.setVisibility(View.GONE);
                }
            }

            //点击事件处理
            if(position==getCount()-1){//减号点击事件处理
                holder.photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!mIsDeleteModel){
                            mIsDeleteModel=true;
                            notifyDataSetChanged();
                        }
                    }
                });
            }else if(position==getCount()-2){//加号点击事件处理
                holder.photo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //留给被调用的接口处理
                        mOnGroupDetailListener.onAddMembers();
                    }
                });
            }else {
                holder.delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //留给被调用的接口处理
                        mOnGroupDetailListener.onDeleteMember(userInfo);
                    }
                });
            }


        }else {//表示群成员
            if(position==getCount()-1||position==getCount()-2){
                convertView.setVisibility(View.GONE);
            }else{
                convertView.setVisibility(View.VISIBLE);
                holder.name.setText(userInfo.getName());
                holder.photo.setImageResource(R.drawable.minis_sign);
                holder.delete.setVisibility(View.GONE);
            }
        }

        //返回view
        return null;
    }

    //获取是否是删除模式
    public boolean ismIsDeleteModel() {
        return mIsDeleteModel;
    }

    public void setmIsDeleteModel(boolean mIsDeleteModel) {
        this.mIsDeleteModel = mIsDeleteModel;
    }

    private class ViewHolder{
        private ImageView photo;
        private ImageView delete;
        private TextView name;
    }

    public interface OnGroupDetailListener{
        void onAddMembers();
        void onDeleteMember(UserInfo user);
    }
}
